package java;

public class Question28 {
    public static void main(String[] args) {
        System.out.println(strStr("a","a"));
    }
    public static int strStr(String haystack, String needle) {
        if (needle == null || needle.length() < 1) {
            return 0;
        }
        if (haystack == null || haystack.length() < 1) {
            return -1;
        }
        int needleLength = needle.length();
        for (int i = 0; i < haystack.length() - needleLength + 1; i ++){
            if (haystack.charAt(i) == needle.charAt(0) && isMatch(i,haystack,needle)){
                return i;
            }
        }
        return -1;
    }

    private static boolean isMatch(int i, String haystack, String needle) {
        for (int j = 0; j < needle.length() ; j ++){
            if (haystack.charAt(i + j) != needle.charAt(j)){
                return false;
            }
        }
        return true;
    }
}
